package cn.com.common.tools;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {
	/**
	 * match email
	 * @param inputStr
	 * @return
	 */
	public static Boolean isEmail(String inputStr){
		Boolean boolemail=false;
		String regex="\\w+?@\\w+?(.\\w+)+";
		boolemail=matchRegex(inputStr,regex);
		return boolemail;
	}
	/**
	 * match number
	 * @param inputStr
	 * @return
	 */
	public static Boolean isNum(String inputStr){
		Boolean boolNum=false;
		String regex="\\d+";
		boolNum=matchRegex(inputStr,regex);
		return boolNum;
	}
	/**
	 * matcha phone number
	 * @param inputStr
	 * @return
	 */
	public static Boolean isPhonnum(String inputStr){
		Boolean boolPhonenum=false;
		String regex="^((13[0-9])|(14[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
		boolPhonenum=matchRegex(inputStr,regex);
		return boolPhonenum;
	}
	/**
	 * match pID
	 * @param inputStr
	 * @return
	 */
	public static Boolean isPID(String inputStr){		
		Boolean boolPID=false;
		String regex="(^\\d{17}(x|X|[0-9])$)|(^\\d{14}(x|X|[0-9])$)";
		boolPID=matchRegex(inputStr,regex);
		return boolPID;
	}
	/**
	 * matcha chiness
	 * @param inputStr
	 * @return
	 */
	public static Boolean isChiness(String inputStr){
		Boolean boolChiness=false;
		String regex="^[\u4e00-\u9fa5],{0,}$";
		boolChiness=matchRegex(inputStr,regex);
		return boolChiness;
	}
	/**
	 * persion define match
	 * @param inputStr
	 * @param regex
	 * @return
	 */
	public static Boolean matchRegex(String inputStr,String regex){
		Boolean boolstr=false;		
		if(inputStr==null||"".equals(inputStr)){
			return boolstr;
		}
		Pattern pattern=Pattern.compile(regex);
	    Matcher  match=pattern.matcher(inputStr);
	    if(match.matches()){
	    	boolstr=true;
	    }
		return boolstr;
	}
	/**
	 * get email
	 * @param inputStr
	 * @return
	 */
	public static List<String> getEmails(String inputStr){
		List<String> listEmails=new ArrayList<String>();
		String regex="\\w+?@\\w+?(.\\w+)+";
		listEmails=getRegex(inputStr,regex);
		return listEmails;
	}
	/**
	 * get number
	 * @param inputStr
	 * @return
	 */
	public static List<String> getNum(String inputStr){
		List<String> listNum=new ArrayList<String>();
		String regex="\\d+";
		listNum=getRegex(inputStr,regex);
		return listNum;
	}
	/**
	 * get phone number
	 * @param inputStr
	 * @return
	 */
	public static List<String> getPhonenum(String inputStr){
		List<String> listPhonenum=new ArrayList<String>();
		String regex="((13[0-9])|(14[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}";
		listPhonenum=getRegex(inputStr,regex);
		return listPhonenum;
	}
	/**
	 * get person id
	 * @param inputStr
	 * @return
	 */
	public static List<String> getPID(String inputStr){
		List<String> listPID=new ArrayList<String>();
		String regex="(\\d{17}(x|X))|(\\d{18})|(\\d{15})|(\\d{14}(x|X))";
		listPID=getRegex(inputStr,regex);
		return listPID;
	}
	/**
	 * p define get string
	 * @param inputStr
	 * @param regex
	 * @return
	 */
	public static List<String> getRegex(String inputStr,String regex){
		List<String> listEmails=new ArrayList<String>();
		if(inputStr==null||"".equals(inputStr)){
			return listEmails;
		}
		Pattern pattern=Pattern.compile(regex);
	    Matcher  match=pattern.matcher(inputStr);
	   while(match.find()){
		   listEmails.add(match.group());
	   }
		return listEmails;
	}
}
